import flash.display.Bitmap;
import flash.display.BitmapData;

import mx.collections.*;
import mx.core.FlexGlobals;
import mx.formatters.NumberBaseRoundType;
import mx.formatters.NumberFormatter;

import zakladki.alert;

[Bindable]
public var tempArrayCollection:ArrayCollection = new ArrayCollection();

[Bindable]
public var C1NameXC:XMLListCollection;
[Bindable]
public var RNameXC:XMLListCollection;
[Bindable]
public var AC:ArrayCollection;
//Zmienne do obsługi mapy 
private var bm:Bitmap;
private var bmd:BitmapData;
//Państwo nad ktorym obecnie jest myszka
private var Country:String;
//Państwo zaznaczone na liście rozwijanej
private var Country_selected:String;
private var region_selected:int;
private var region_selected_str:String;
//kod języka - na razie tylko polska
private var lang:String = "PL";
private var ilosc_krajow:int;



/************************************************************************************************/
//Funkcja inicjująca pierwszą zakładkę, wczytuje kolekcję XML do pola combobox
private function start_main():void {
    //Ilość rekordów z kolekcji XML
    var ilosc:int = new int(0);
    var Countries:Array = new Array(100);
    C1NameXC = new XMLListCollection(krajXML.Continent.Country.Name_pl);
    ilosc = C1NameXC.length;
    ilosc_krajow = ilosc
    for (var i:int = 0; i < ilosc; i++) {
        Countries[i] = krajXML.Continent.Country.Name[i];
    }
}
/***********************************************************************************************/
//Funkcja steruje widokami - czy standardowy czy zaawansowany ?
private function tryb_widoku(podstawowy:Boolean):void {
    if (podstawowy = true) {
        image1.visible = false;
        canvas1.visible = false;
        image2.visible = true;
        canvas2.visible = true;
    }
    else {
        image1.visible = true;
        canvas1.visible = true;
        image2.visible = false;
        canvas2.visible = false;
    }
}


//Funkcja pobiera z XML temeraturę i steruje wyświetlaniem kropek na wykresie Temperatury
//na stronie wyboru państwa
private function plotTemperatures(isMainRegion:Boolean):void {
    var tempArray:Array = new Array();
    tempArray = getCountryTemperatures(isMainRegion);

    tempArrayCollection = new ArrayCollection();
    for(var i:int = 0;i < tempArray.length;i++){
        var item:Object = {Month:i+1,Temperature:tempArray[i]};
        tempArrayCollection.addItem(item);
    }

    /*if (tempArray[0] != 0) {
        temp1.y = 131.9 - (int(tempArray[0]) * 2);
        temp1.visible = true;
        temp2.y = 131.9 - (int(tempArray[1]) * 2);
        temp2.visible = true;
        temp3.y = 131.9 - (int(tempArray[2]) * 2);
        temp3.visible = true;
        temp4.y = 131.9 - (int(tempArray[3]) * 2);
        temp4.visible = true;
        temp5.y = 131.9 - (int(tempArray[4]) * 2);
        temp5.visible = true;
        temp6.y = 131.9 - (int(tempArray[5]) * 2);
        temp6.visible = true;
        temp7.y = 131.9 - (int(tempArray[6]) * 2);
        temp7.visible = true;
        temp8.y = 131.9 - (int(tempArray[7]) * 2);
        temp8.visible = true;
        temp9.y = 131.9 - (int(tempArray[8]) * 2);
        temp9.visible = true;
        temp10.y = 131.9 - (int(tempArray[9]) * 2);
        temp10.visible = true;
        temp11.y = 131.9 - (int(tempArray[10]) * 2);
        temp11.visible = true;
        temp12.y = 131.9 - (int(tempArray[11]) * 2);
        temp12.visible = true;
    }
    else {
        temp1.visible = false;
        temp2.visible = false;
        temp3.visible = false;
        temp4.visible = false;
        temp5.visible = false;
        temp6.visible = false;
        temp7.visible = false;
        temp8.visible = false;
        temp9.visible = false;
        temp10.visible = false;
        temp11.visible = false;
        temp12.visible = false;
    }*/
}
//Funkcja pobiera dane z XML na temat zużycia surowców i wyświetla je w postaci słupków 
//na stronie wyboru państwa
private function pobierz_dane_widok_prosty():void {
    var PKB:Number = new Number(krajXML.Continent.Country.(Name == Country_name).PKBPerCapita);
    var Emisja:Number = new Number(krajXML.Continent.Country.(Name == Country_name).EmissionPerCapita);
    var Zuzycie_pradu:Number = new Number();
    var Zuzycie_produ_na_produkcje:Number = new Number();
    var Zuzycie_calk_pradu:Number = new Number();
    var podstawa:Number = new Number(508);
    var max_dlugosc:Number = new Number(213);
}


/************************** Do obsługi ruchów myszki nad mapą ***************************************/
//Funkcja pobiera aktualny kolor nad którym jest myszka i wyświetla dane na temat państwa
//na stronie wyboru państwa
public function kolor(kolor:String):void {

    var number:int;
    var objVar:Object = {kod_koloru:kolor ,country:"" , nazwa:""};
    flagi(objVar);

    Country = objVar.country;

    if (Country != "") {
        cboRegion.visible = false;
        lblRegion.visible = false;
        cboRegion1.visible = false;
        lblRegion1.visible = false;

        RNameXC = new XMLListCollection(krajXML.Continent.Country.(Name == Country).Region.Name_pl);
        number = RNameXC.length;
        if (number > 1) {
            cboRegion.visible = true;
            lblRegion.visible = true;
            cboRegion1.visible = true;
            lblRegion1.visible = true;
            if (Country_selected == Country) {
                cboRegion.selectedIndex = region_selected;
                cboRegion1.selectedIndex = 0;
            } else {
                cboRegion.selectedIndex = 0;
                cboRegion1.selectedIndex = 0;
            }
        }
        else {
            cboRegion.visible = false;
            lblRegion.visible = false;
            cboRegion1.visible = false;
            lblRegion1.visible = false;
        }

        plotTemperatures(true);
    }
    else if (Country_selected != "") {
        cboRegion.visible = false
        lblRegion.visible = false
        cboRegion1.visible = false
        lblRegion1.visible = false
        Country = Country_selected;
        RNameXC = new XMLListCollection(krajXML.Continent.Country.(Name == Country).Region.Name_pl);
        number = RNameXC.length;

        if (number > 1) {
            cboRegion.visible = true;
            lblRegion.visible = true;
            cboRegion.selectedIndex = 0;
            cboRegion1.visible = true;
            lblRegion1.visible = true;
            cboRegion1.selectedIndex = region_selected;
        }
        else {
            cboRegion.visible = false;
            lblRegion.visible = false;
            cboRegion1.visible = false;
            lblRegion1.visible = false;
        }
        var objVar1:Object = {nazwa_panstwa:Country_selected ,country:"" , nazwa:""};

        flagi_nazwa_panstwa(objVar1);//shows flag on plot panel


        plotTemperatures(true);
    }
}
//Po kliknięciu w dany kraj funkcja ustawia listę krajów i zapisuje wybór
//Jeśli myszka jest poza mapą wyświetlane są dane dla klikniętego kraju
//na stronie wyboru państwa


public function cboRegion_change():void {
    region_selected = cboRegion.selectedIndex;
    region_selected_str = cboRegion.text;
    plotTemperatures(false);
}
public function cboRegion_change1():void {
    region_selected = cboRegion1.selectedIndex;
    region_selected_str = cboRegion1.text;
    plotTemperatures(false);
}


/****************************************************************************************************/
/****************************Do obsługi listy i pola tekstowego nad nią******************************/
//Funkcja fitruje listę państw na podstawie wprowadzonych liter do pola tekstowego
//na stronie wyboru państwa
private function Zmianatxt():void {
    var txt:String = new String("");
    txt = txtSearch.text;

    if (txtSearch.text == "") {
        C1NameXC.filterFunction = null;
    }
    else if (txtSearch.text != "") {
        C1NameXC.filterFunction = Filtruj;
    }
    C1NameXC.refresh();

}
//Funkcja filtruje obiekty XMLListCollection - wspomaga funkcję Zmianatxt
public function Filtruj(item:Object):Boolean {
    var wynik:Boolean = new Boolean(false)

    if (item.toString().toLowerCase().search(txtSearch.text.toLowerCase()) != -1) {
        wynik = true;
    }
    else {
        wynik = false;
    }
    return wynik;
}

//Funkcja obsługuje zmiany w comboboxie
//na stronie wyboru państwa

/****************************************************************************************************/
//Do przekazania klasie państwo wszystkich danych

private function getCountryTemperatures(mainRegion:Boolean):Array {

    var temperatury:Array = new Array();
    var Country_name:String = ""
    var region_name:String = "CC";
    if (region_selected_str != null && region_selected_str != "" && !mainRegion) {
        region_name = region_selected_str;
    }
    var t_test:String = new String("")
    if (Country != "") {
        Country_name = Country;
    }
    else if (Country_selected != "") {
        Country_name = Country_selected;
    }
    else {
        Country_name = "";
    }


    if (Country_name != "" && Country_name != null) {
        if (region_name == "CC") {
            t_test = krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T1;

            if (t_test != "") {
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T1);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T2);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T3);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T4);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T5);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T6);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T7);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T8);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T9);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T10);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T11);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name == region_name).T12);
            }
            else {
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T1);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T2);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T3);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T4);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T5);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T6);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T7);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T8);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T9);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T10);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T11);
                temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name1 == region_name).T12);
            }

        }
        else {
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T1);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T2);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T3);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T4);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T5);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T6);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T7);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T8);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T9);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T10);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T11);
            temperatury.push(krajXML.Continent.Country.(Name == Country_name).Region.(Name_pl == region_name).T12);
        }
    }


    return temperatury;
}


private function pobierz_populacje(panstwo:String):Array {
    var pop:Array = new Array();
    pop.push(krajXML.Continent.Country.(Name == panstwo).Pop0);
    pop.push(krajXML.Continent.Country.(Name == panstwo).Pop1);
    pop.push(krajXML.Continent.Country.(Name == panstwo).Pop2);
    pop.push(krajXML.Continent.Country.(Name == panstwo).Pop3);
    pop.push(krajXML.Continent.Country.(Name == panstwo).Pop4);
    pop.push(krajXML.Continent.Country.(Name == panstwo).Pop5);
    return pop;
}



private function panstwo_EconomyEff(panstwo:String):Number {
    var EconomyEff:Number = new Number(0);
    EconomyEff = krajXML.Continent.Country.(Name == panstwo).EconomyEff;
    if (EconomyEff == 0) {
        EconomyEff = 1;
    }
    return EconomyEff;
}

private function panstwo_ForestCut2005(panstwo:String):Number {
    return krajXML.Continent.Country.(Name == panstwo).ForestCut2005;
}

private function panstwo_Forest2005(panstwo:String):Number {
    var Forest2005:Number = new Number(0);
    Forest2005 = krajXML.Continent.Country.(Name == panstwo).ForestCut2005;
    if (Forest2005 == 0) {
        Forest2005 = 1000;
    }
    return Forest2005;
}



import mx.controls.Alert;

import AS3.Country;

import mx.core.Application;


